package com.beviswong.themedemo

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class NightActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_night)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        initViews()
    }

    private fun initViews() {
        findViewById<View>(R.id.tv_follow_system).setOnClickListener {
            NightManager.saveNightMode(this, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
        }
        findViewById<View>(R.id.tv_night).setOnClickListener {
            NightManager.saveNightMode(this, AppCompatDelegate.MODE_NIGHT_YES)
        }
        findViewById<View>(R.id.tv_no_night).setOnClickListener {
            NightManager.saveNightMode(this, AppCompatDelegate.MODE_NIGHT_NO)
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        recreate() // 重新创建Activity，使深色模式资源立即生效
    }

    companion object {
        fun start(mainActivity: MainActivity) {
            mainActivity.startActivity(Intent(mainActivity, NightActivity::class.java))
        }
    }
}